﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;

namespace QLNSClient
{
    public partial class Frm_CBPCTN : DevExpress.XtraEditors.XtraForm
    {

        QLNSServiceReference.QLNSServiceClient sv = new QLNSServiceReference.QLNSServiceClient();
        
        public Frm_CBPCTN()
        {
            InitializeComponent();
        }


        public void LoadData()
        {
            btn_Them.Enabled = false;
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = false;

            lke_MaCB.Enabled = false;
            txt_TNhientai.Enabled = false;
            txt_TNtieptheo.Enabled = false;

            gv_dsCBPCTN.DataSource = sv.getCBPCTN();
            gridView1.Columns[0].Caption = "Mã cán bộ";
            gridView1.Columns[1].Caption = "Mã phụ cấp thâm niên";
            gridView1.Columns[2].Caption = "Quyết định số";
            gridView1.Columns[3].Caption = "Ngày ký";
            gridView1.Columns[4].Visible=false;

            lke_MaCB.Properties.DataSource = sv.getCB();

            if (gridView1.DataRowCount == 0)
            {
                MessageBox.Show("Chưa có dữ liệu!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
                //for (int i = 0; i < gridView1.RowCount; i++)
                //{
                //    if (gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString() == lke_MaCB.EditValue.ToString())
                //    {
                //        txt_Quyetdinh.Text=gridView1.GetRowCellValue(i, gridView1.Columns[2]).ToString();
                //        de_Ngayky.DateTime = DateTime.Parse(gridView1.GetRowCellValue(i, gridView1.Columns[3]).ToString());
                //    }
                //}

        }
       
        private void Frm_CBPCTN_Load(object sender, EventArgs e)
        {
            LoadData();
            if (Frm_dsCoThamNien.isViewCoThamNien == true && Frm_SAPTANGTN.isSapTangTN == false)
            {
                lke_MaCB.EditValue = Frm_dsCoThamNien.sendViewTTThamNien;
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    btn_Luu.Enabled = false;
                    txt_Quyetdinh.Enabled = false;
                    de_Ngayky.Enabled = false;
                    buttonEditFileQD.Enabled = false;
                    btn_Sua.Enabled = true;
                    if (gridView1.GetRowCellValue(i, gridView1.Columns[0]).ToString() == lke_MaCB.EditValue.ToString())
                    {
                        txt_Quyetdinh.Text = gridView1.GetRowCellValue(i, gridView1.Columns[2]).ToString();
                        de_Ngayky.DateTime = DateTime.Parse(gridView1.GetRowCellValue(i, gridView1.Columns[3]).ToString());
                    }
                }
            }
            if (Frm_SAPTANGTN.isSapTangTN == true && Frm_dsCoThamNien.isViewCoThamNien == false)
            {
                btn_Luu.Enabled = true;
                lke_MaCB.EditValue = Frm_SAPTANGTN.ValueRow;
            }
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = true;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;
            btn_Huy.Enabled = true;
           
            txt_Quyetdinh.Enabled = true;
            de_Ngayky.Enabled = true;
            buttonEditFileQD.Enabled = true;

            
            txt_Quyetdinh.Text = "";
            de_Ngayky.Text = "";
            buttonEditFileQD.Text = null;
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            btn_Huy.Enabled = false;
            //btn_Them.Enabled = true;

            string tempMaCB = "";
            string tempTNHienTai = txt_TNhientai.Text;
            string tempTHTiepTheo = txt_TNtieptheo.Text;
            string tempQuyetDinhTN = txt_Quyetdinh.Text;
            DateTime tempNgayKyTN;
            if (de_Ngayky.Text == "")
            {
                tempNgayKyTN = DateTime.Today.Date;
            }
            else
            {
                tempNgayKyTN = de_Ngayky.DateTime;
            }
            if (lke_MaCB.EditValue.ToString() == "")
            {
                MessageBox.Show("Bạn chưa chọn Cán bộ!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                tempMaCB = null;
                lke_MaCB.Focus();
            }
            else
            {
                tempMaCB=lke_MaCB.EditValue.ToString();
            }

            
            QLNSServiceReference.CANBO_PCTHAMNIEN cbpctn = new QLNSServiceReference.CANBO_PCTHAMNIEN();
            if (tempMaCB != null)
            {
                //==============them or update vao bang hstn hien tai================
                
                
                //===========cb pctn==============
                cbpctn.Ma_CB = tempMaCB;
                
                cbpctn.QuyetDinhTN = tempQuyetDinhTN;
                cbpctn.NgayKyTN = tempNgayKyTN;

                if (buttonEditFileQD.Text.Trim() != "")
                {
                    FileStream frm;
                    frm = new FileStream(@buttonEditFileQD.Text.Trim(), FileMode.Open, FileAccess.Read);
                    byte[] picbyte = new byte[frm.Length];
                    frm.Read(picbyte, 0, System.Convert.ToInt32(frm.Length));
                    frm.Close();
                    frm.Dispose();
                    cbpctn.FileQuyetDinhTN = picbyte;

                }
                else
                {
                    byte[] picbyte = new byte[0];
                    cbpctn.FileQuyetDinhTN = picbyte;

                }
                if (demsua == 1)
                {

                    QLNSServiceReference.HSTN_HIENTAI tnht = new QLNSServiceReference.HSTN_HIENTAI();
                    tnht.Ma_CB = tempMaCB;
                    tnht.HoTen = lke_MaCB.Text;
                    tnht.GiaTriTN = Int32.Parse(tempTHTiepTheo);
                    tnht.QuyetDinhTN = tempQuyetDinhTN;
                    tnht.NgayTN = tempNgayKyTN;

                    int kq1 = sv.addTNHienTai(tnht);
                    if (kq1 == -1)
                    {
                        tnht.GiaTriTN = Int32.Parse(txt_TNhientai.Text);
                        sv.updateTNHienTai(tnht);
                    }
                    DialogResult dr = MessageBox.Show("Bạn có chắc muốn cập nhật thông tin thâm niên của cán bộ '" + lke_MaCB.Text + "'?", "CẢNH BÁO", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dr == DialogResult.OK)
                    {
                        cbpctn.Ma_PCTN = Int32.Parse(txt_TNhientai.Text);
                        sv.updateCBPCTN(cbpctn);
                        LoadData();
                        demsua = 0;
                        MessageBox.Show("Cập nhật thông tin thâm niên của cán bộ '" + lke_MaCB.Text + "' thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    }
                }
                else
                {
                    QLNSServiceReference.HSTN_HIENTAI tnht = new QLNSServiceReference.HSTN_HIENTAI();
                    tnht.Ma_CB = tempMaCB;
                    tnht.HoTen = lke_MaCB.Text;
                    tnht.GiaTriTN = Int32.Parse(tempTHTiepTheo);
                    tnht.QuyetDinhTN = tempQuyetDinhTN;
                    tnht.NgayTN = tempNgayKyTN;

                    int kq1 = sv.addTNHienTai(tnht);
                    if (kq1 == -1)
                    {
                        tnht.GiaTriTN = Int32.Parse(txt_TNtieptheo.Text);
                        sv.updateTNHienTai(tnht);
                    }
                    
                    cbpctn.Ma_PCTN = Int32.Parse(tempTHTiepTheo);
                    int kq = sv.addCBPCTN(cbpctn);
                    if (kq != -1)
                    {
                        LoadData();
                        MessageBox.Show("Thêm quyết định thâm niên cho cán bộ '"+lke_MaCB.Text+"' lên '"+txt_TNtieptheo.Text+"'%  thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            demsua = 0;
            
            btn_Luu.Enabled = false;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = false;
            btn_Xoa.Enabled = false;
            btn_Sua.Enabled = false;

            txt_Quyetdinh.Enabled = false;
            de_Ngayky.Enabled = false;
            buttonEditFileQD.Enabled = false;
        }


        int demsua = 0;
        private void btn_Sua_Click(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount > 0)
            {
                demsua = 1;

                btn_Luu.Enabled = true;
                btn_Them.Enabled = false;
                btn_Huy.Enabled = true;
                btn_Sua.Enabled = false;

                txt_Quyetdinh.Enabled = true;
                de_Ngayky.Enabled = true;
                buttonEditFileQD.Enabled = true;
            }
            else
            {
                MessageBox.Show("Không có dữ liệu để sửa!", "Cảnh báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {

        }

        private void gv_dsCBPCTN_Click(object sender, EventArgs e)
        {
            btn_Luu.Enabled = false;
            btn_Sua.Enabled = true;
            btn_Huy.Enabled = false;
            btn_Them.Enabled = false;

            lke_MaCB.Enabled = false;
            txt_Quyetdinh.Enabled = false;
            de_Ngayky.Enabled = false;
            buttonEditFileQD.Enabled = false;

            try
            {
                lke_MaCB.EditValue = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[0]);
                txt_Quyetdinh.Text = gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[2]);
                de_Ngayky.DateTime = DateTime.Parse(gridView1.GetFocusedRowCellDisplayText(gridView1.Columns[3]));
               
              }
            catch { }
        }

        private void buttonEditFileQD_Click(object sender, EventArgs e)
        {

        }


        
        private void gv_dsCBPCTN_DoubleClick(object sender, EventArgs e)
        {

        }


        XtraFormPdfViewer fqd;
        private void buttonEditFileQD_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {

                FileDialog fdg = new OpenFileDialog();
                fdg.InitialDirectory = @"d:\";
                fdg.Filter = "File PDF(*.pdf)|*pdf";
                if (fdg.ShowDialog() == DialogResult.OK)
                {

                    buttonEditFileQD.Text = fdg.FileName;
                    if (fqd == null || fqd.IsDisposed)
                    {
                        fqd = new XtraFormPdfViewer(fdg.FileName);
                        fqd.Show();
                    }
                    else
                        fqd.Activate();
                }
                fdg = null;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        XtraFormPdfViewer frmpdf;
        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.DataRowCount > 0)
            {
                if ((byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[4]) != null && ((byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[4])).Length != 8000)
                    if (frmpdf == null || frmpdf.IsDisposed)
                    {

                        byte[] pdfbyte = (byte[])gridView1.GetFocusedRowCellValue(gridView1.Columns[4]);
                      
                        if (pdfbyte.Length != 0)
                        {
                            frmpdf = new XtraFormPdfViewer(pdfbyte);
                            frmpdf.Show();
                        }
                        else
                        {
                            frmpdf.Activate();
                        }
                    }
            }
        }

        string macb;
        private void lke_MaCB_EditValueChanged(object sender, EventArgs e)
        {
            macb = lke_MaCB.EditValue.ToString();
            List<QLNSServiceReference.HSTN_HIENTAI> listTNHienTai = new List<QLNSServiceReference.HSTN_HIENTAI>(sv.getHSTNHientai(macb));
            int  TNHienTai=0;
            if (listTNHienTai.Count > 0)
            {
                TNHienTai = listTNHienTai[0].GiaTriTN;
                txt_TNhientai.Text = TNHienTai.ToString();

            }
            else
                txt_TNhientai.Text = TNHienTai.ToString();
            if (txt_TNhientai.Text == "0")
            {
                txt_TNtieptheo.Text = "5";
            }
            else
            {
                txt_TNtieptheo.Text = (TNHienTai + 1).ToString();
            }
        }

        private void lke_dmDonvi_EditValueChanged(object sender, EventArgs e)
        {
          
        }

        private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {

        }

        private void Frm_CBPCTN_FormClosing(object sender, FormClosingEventArgs e)
        {
         //   DialogResult = DialogResult.OK;
            //Frm_dsCoThamNien.ActiveForm.Refresh();
            capnhat();
            
            
        }
        public delegate void CapNhatDL();
        public CapNhatDL capnhat;
    }

}